<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 11, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RClassesImporter::cls("html.AbstractHtmlCheckBox" );

class HtmlSqlCheckBox extends AbstractHtmlCheckBox {
	
	var $_db = null;
	var $_sql = null;
	var $_dataFilled = false;
	 
	/**
	 * Constructor
	 *
	 * @param string $inputName
	 * @param string $sql the string with the first is key, second is value "SELECT key, value FROM xxx"
	 * @param array $valueOrArrayContain
	 * @param Database $db database
	 */
	function __construct($inputName, $sql, $valueOrArrayContain = null, $db = null) {
		parent::__construct ( $inputName, $valueOrArrayContain );
		
		if (empty ( $db )) {
			$db = &SysDatabase::getInstance ();
		}
		$this->_db = &$db;
		$this->_sql = $sql;
	}
	
	/**
	 * Set the sql string
	 *
	 * @param string $sql
	 */
	function setSql($sql) {
		$this->_dataFilled = false;
		$this->_sql = $sql;
	}
	
	/**
	 * Create HTML text for the combobox
	 *
	 * @param $extAttributes
	 * @param $htmlIdPrefix
	 * @param $wrapper string wrapper is the name tag of wrapper maye: 'div', 'span'
	 * @param $wraperAttributes the attribute for the wrapper
	 * @return string
	 */
	function createCheckBox($extAttributes = "", $htmlIdPrefix = "", $wrapper=null, $wraperAttributes = null) {
		if (!$this->_dataFilled) {
			$db	= $this->_db;
			$db->setQuery ( $this->_sql );
			$result = $db->loadRowList ();
			
			foreach ( $result as $rec ) {
				$key = $rec [0];
				$val = $rec [1];
				$this->$key = $val;
			}
			
			$this->_dataFilled = true;
		}
		return parent::createCheckBox($extAttributes, $htmlIdPrefix, $wrapper, $wraperAttributes);
	}
	
}
?>